﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace Reactor.Services.AdvisoryEmailService.Data
{
    public class AdvisoryChannelToEmailMapProvider
    {
        private readonly string _connectionString;

        /// <summary>
        /// Initializes a new instance of the <see cref="AdvisoryChannelToEmailMapProvider"/> class.
        /// </summary>
        /// <param name="connectionString">The connection string.</param>
        public AdvisoryChannelToEmailMapProvider(string connectionString)
        {
            if (string.IsNullOrEmpty(connectionString))
                throw new ArgumentNullException("connectionString");

            _connectionString = connectionString;
        }

        public List<AdvisoryPublicationToEmailMap> GetMaps()
        {
            var results = new List<AdvisoryPublicationToEmailMap>();

            using (var sqlConnection = new SqlConnection(_connectionString))
            {
                using (var cmd = new SqlCommand("dbo.rsp_GetAllAdvisoryTopicToEmailMaps"))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    sqlConnection.Open();
                    cmd.Connection = sqlConnection;

                    using (var rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                    {
                        if (rdr != null)
                        {
                            while (rdr.Read())
                            {
                                var ccColumnOrdinal = rdr.GetOrdinal("CcEmailAddresses");
                                var advisoryTopic = rdr.GetString(rdr.GetOrdinal("AdvisoryTopic"));
                                var sendToEmailAddresses = rdr.GetString(rdr.GetOrdinal("SendToEmailAddresses"));
                                var ccEmailAddresses = rdr.IsDBNull(ccColumnOrdinal) ? null : rdr.GetString(ccColumnOrdinal);
                                var subjectFormatString = rdr.GetString(rdr.GetOrdinal("SubjectFormatString"));
                                var bodyFormatString = rdr.GetString(rdr.GetOrdinal("BodyFormatString"));

                                var map = new AdvisoryPublicationToEmailMap
                                              {
                                                  AdvisoryChannel = advisoryTopic,
                                                  BodyFormatString = bodyFormatString,
                                                  SubjectFormatString = subjectFormatString,
                                                  Cc = ccEmailAddresses,
                                                  SendTo = sendToEmailAddresses
                                              };
                                results.Add(map);
                            }
                        }
                    }
                }
            }

            return results;
        }
    }
}
